<!--  货补池费用管理-->
<!--  货补池管理-->
<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <search ref='search' :config='searchConfig' @search='search' @getHeight='getHeight' />

    <!-- table区域-begin -->
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        v-bind='vexConfig'
        :align='left'
        :border='true'
        :checkbox-config='checkboxConfig'
        :columns='columns'
        :data='dataSource'
        :height='pageHeight-seachHeight-94'
        :loading='loading'
        :resizable='true'
        :showOverflow='true'
        :toolbar-config='tableToolbar'
        size='mini'
        @checkbox-change='selectChangeEvent'
        @checkbox-all='selectChangeEvent'
      >
        <template #toolbar_buttons>
          <!-- 操作按钮区域 -->
          <div class='table-operator'>
            <a-button type='primary' @click='handleAdd'>上账</a-button>
            <a-button type='primary' @click="downExportXls('cn_fee_pool_service')">导出</a-button>
          </div>
        </template>
        <template #poolCode='{ row,rowIndex }'>
          <a @click='handleEdit(row,"detail")'>{{ row.poolCode }}</a>
        </template>
        <template #status='{ row }'>
          <a-switch :checked="row.status" size="small" @change="handleStatus(row,$event)"/>
        </template>
        <template #action="{ row:record }">
          <a @click='handleSeeDetail(record)'>明细</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多
              <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click='handleEdit(record)'>调整</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleEdit(record,'003')">冻结</a>
              </a-menu-item>
              <a-menu-item>
                <a @click="handleEdit(record,'009')">解冻</a>
              </a-menu-item>
<!--              <a-menu-item>-->
<!--                <a @click="stateChange(record,'009')">启用</a>-->
<!--              </a-menu-item>-->
<!--              <a-menu-item>-->
<!--                <a @click="stateChange(record,'003')">停用</a>-->
<!--              </a-menu-item>-->
            </a-menu>
          </a-dropdown>
        </template>
        <template #pager>
          <vxe-pager
            :currentPage.sync='ipagination.current'
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :page-size.sync='ipagination.pageSize'
            :pageSizes='ipagination.pageSizeOptions'
            :total='ipagination.total'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!-- table区域-end -->

    <roolModaldetail ref='detailForm'></roolModaldetail>
    <repTankModal ref='modalForm' @ok='modalFormOk'></repTankModal>
  </a-card>
</template>

<script>
import { PageListMixin } from '@/mixins/PageListMixin'
import { getAction, postAction } from '@api/manage'
import repTankModal from '@/views/tmr/costCenter/modules/repTankModal'
import HpayselectCom from '@/components/cn-components/HpayselectCom'
import roolModaldetail from '@/views/tmr/costCenter/modules/roolModaldetail'


export default {
  name: 'repTankCost',
  description: '货补池费用管理页面',
  mixins: [PageListMixin],
  components: {
    repTankModal,
    HpayselectCom,
    roolModaldetail
  },
  data() {
    return {
      permflag: 'cn_fee_page_impl',
      loading: false,
      searchConfig: [
        {
          type: 'input',
          name: 'custCode',
          placeholder: '客户编码'
        },
        {
          type: 'input',
          name: 'custName',
          placeholder: '客户名称'
        },
        {
          type: 'input',
          name: 'productCode',
          placeholder: '商品编码'
        },
        {
          type: 'input',
          name: 'productName',
          placeholder: '商品名称'
        },
        {
          type: 'input',
          name: 'orgName',
          placeholder: '组织名称'
        },
        {
          type: 'input',
          name: 'orgCode',
          placeholder: '组织编码'
        },
        {
          dictCode: 'fee_manage_type',
          type: 'j-select',
          name: 'feeManageType',
          placeholder: '费用池类型'
        },
      ],
      // 表头
      columns: [
        { type: 'checkbox', width: 50, fixed: null ,field: 'a',},
        {
          title: '货补池编码',
          field: 'poolCode',
          minWidth: 100,
          slots: { default: 'poolCode' },
        },
        {
          title: '客户编码',
          minWidth: 80,
          field: 'custCode'
        },
        {
          title: '客户名称',
          minWidth: 80,
          field: 'custName'

        },
        {
          title: '所属组织',
          minWidth: 80,
          field: 'orgName'
        },
        {
          title: '费用池类型',
          minWidth:80,
          field: 'feeManageTypeStr',
        },
        {
          title: '货补商品层级编码',
          minWidth: 160,
          field: 'productHierarchyCode'
        },
        {
          title: '货补商品层级名称',
          minWidth: 160,
          field: 'productHierarchyName'
        },
        {
          title: '货补商品编码',
          minWidth: 120,
          field: 'productCode'
        },
        {
          title: '货补商品名称',
          minWidth: 120,
          field: 'productName'
        },
        {
          title: '上账金额',
          minWidth: 80,
          field: 'accountFee'
        },
        {
          title: '冻结金额',
          minWidth: 80,
          field: 'frozenFee'
        },
        {
          title: '已用金额',
          minWidth: 80,
          field: 'usedFee'
        },
        {
          title: '可用余额',
          minWidth: 80,
          field: 'balance'
        },
        {
          title: '状态',
          minWidth: 80,
          fixed: 'right',
          field: 'status',
          slots: { default: 'status' },
        },
        {
          title: '操作',
          field: 'action',
          fixed: 'right',
          width: 120,
          slots: { default: 'action' },
        }
      ],
      url: {
        list: window._CONFIG['domianURL'] + '/fee/cnFeePool/findPageList',
        check: window._CONFIG['domianURL'] + 'report/tpmActExecuteReport/examineReport',
        detail: window._CONFIG['domianURL'] + '/fee/cnFeePool/getInfo',
        status: window._CONFIG['domianURL'] + '/fee/cnFeePool/startOrStop',
        startOrStopUrl: window._CONFIG['domianURL'] + '/fee/cnFeePool/startOrStop',
        exportXlsUrl: window._CONFIG['domianTpmURL'] + 'budget/tpmBudgetFee/exportXls',
        closeOne: window._CONFIG['domianTpmURL'] + '/activity/closeActivity',
        deleteBatch: window._CONFIG['domianTpmURL'] + '/activity/delete'
      }
    }
  },
  methods: {
    stateChange(record, status) {
      let ids = record.id
      let that = this
      this.$confirm({
        title: `确认${status == '009' ? '启用' : '停用'}`,
        content: `是否${status == '009' ? '启用' : '停用'}选中数据?`,
        onOk: function() {
          that.loading = true
          getAction(that.url.status, { id: ids, status }).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.loadData()
              that.onClearSelected()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.loading = false
          })
        }
      })
    },
    handleSeeDetail(record) {
      this.$refs.detailForm.edit({ ...record })
      this.$refs.detailForm.disableSubmit = false
    },
    handleEdit: async function(record, way) {
      if (this.url.detail) {
        const res = await getAction(this.url.detail, { id: record.id })
        if (res.success) {
          record = res.result
        } else {
          this.$message.error(res.message)
        }
      }
      let that = this
      if (way == 'detail') {
        that.$refs.modalForm.edit(record)
        this.$refs.modalForm.title = '查看'
        this.$refs.modalForm.disableSubmit = true
        this.$refs.modalForm.buttonType = true
        this.$refs.modalForm.buttonTypes = true
        this.$refs.modalForm.statusCotrall = false
      } else if (way == '003' || way == '009') {
        record.payType = way == '003' ? '4' : '5'
        that.$refs.modalForm.edit(record, way)
        this.$refs.modalForm.title = way == '003' ? '冻结' : '解冻'
        this.$refs.modalForm.disableSubmit = false
        this.$refs.modalForm.buttonType = false
        this.$refs.modalForm.buttonTypes = true
        this.$refs.modalForm.statusCotrall = true
      } else {
        that.$refs.modalForm.edit(record)
        this.$refs.modalForm.title = '调整'
        this.$refs.modalForm.disableSubmit = false
        this.$refs.modalForm.buttonType = false
        this.$refs.modalForm.buttonTypes = true
        this.$refs.modalForm.statusCotrall = false
      }


    }

  },
  created() {
    this.queryParam.doc = '订货单'

  },
  // beforeMount() {
  //   const draggingMap = {};
  //   this.columns.forEach((col) => {
  //     draggingMap[col.field] = col.width;
  //   });
  //   const draggingState = Vue.observable(draggingMap);
  //   const resizeableTitle = (h, props, children) => {
  //     let thDom = null;
  //     const { key, ...restProps } = props;
  //     console.log(props)
  //     let col;
  //     // 复选框筛选处理
  //     if (key === 'selection-column') {
  //       col = {};
  //     } else {
  //       col = this.columns.find((item) => {
  //         const k = item.field || item.key;
  //         return k === key;
  //       });
  //     }
  //     // 未设置宽度或者设置不可伸缩时不做伸缩处理
  //     if (!col.width || col.noStretch) {
  //       return <th {...restProps}>{children}</th>;
  //     }
  //     const onDrag = (x) => {
  //       draggingState[key] = 0;
  //       // 设置一个伸缩的最小宽度，防止页面错乱
  //       col.width = Math.max(x, col.minWidth || 30);
  //     };
  //     const onDragstop = () => {
  //       draggingState[key] = thDom.getBoundingClientRect().width;
  //     };
  //     return (
  //       <th
  //         {...restProps}
  //         v-ant-ref={(r) => (thDom = r)}
  //         width={col.width}
  //         class="resize-table-th"
  //       >
  //         {children}
  //         <vue-draggable-resizable
  //           key={col.key}
  //           class="table-draggable-handle"
  //           w={10}
  //           x={col.width||draggingState[key]}
  //           z={1}
  //           axis="x"
  //           draggable={true}
  //           resizable={false}
  //           onDragging={onDrag}
  //           onDragstop={onDragstop}
  //         />
  //       </th>
  //     );
  //   };
  //   this.components = {
  //     header: {
  //       cell: resizeableTitle,
  //     },
  //   };
  // },
  watch: {
    // pageHeight(val){
    //   // 为了避免频繁触发resize函数导致页面卡顿，使用定时器
    //   if(!this.timer){
    //     // 一旦监听到的screenWidth值改变，就将其重新赋给data里的screenWidth
    //     this.pageHeight = val
    //     this.timer = true
    //     let that = this
    //     setTimeout(function(){
    //       // 打印screenWidth变化的值
    //       console.log(that.pageHeight)
    //       that.timer = false
    //     },100)
    //   }
    // }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  }
}
</script>

<style lang='less' scoped>
@import '~@assets/less/common.less';

/deep/ .ant-pagination {
  float: right;
}

///deep/ .resize-table-th {
//  position: relative;
//}
///deep/ .table-draggable-handle {
//  height: 100% !important;
//  bottom: 0;
//  left: auto !important;
//  right: -5px;
//  cursor: col-resize;
//  touch-action: none;
//}

</style>